/*
 * KeepAliveReceiver.cpp
 *
 *  Created on: 20/06/2014
 *      Author: Vitor
 */

#include "KeepAliveReceiver.h"

KeepAliveReceiver::KeepAliveReceiver() :
		socket(UDPSocket(3759)) {
}

KeepAliveReceiver::~KeepAliveReceiver() {
}

void KeepAliveReceiver::run() {
	while (Globals::active.get()) {
		UDPPacket incoming_packet;
		if (socket.recv(1024, incoming_packet)) {
			if (incoming_packet.bytes[0] == MessageTypes::KEEP_ALIVE) {
				if (Globals::peers.get().count(incoming_packet.host_ip) == 1) {
					Globals::peers.lock();
					Globals::peers.get()[incoming_packet.host_ip] =
							SDL_GetTicks();
					Globals::peers.unlock();
				}

			}
		}
		SDL_Delay(20);
	}
}
